package cn.lili.modules.promotion.serviceimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.modules.promotion.entity.dos.PromoCode;
import cn.lili.modules.promotion.entity.vos.CouponVO;
import cn.lili.modules.promotion.mapper.PromoCodeMapper;
import cn.lili.modules.promotion.service.CouponService;
import cn.lili.modules.promotion.service.PromoCodeService;
import cn.lili.modules.promotion.service.PromotionGoodsService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 优惠码业务层实现
 *
 * @author moxingjian
 * @version 1.0
 * @date 2023-10-30 14:15
 * <p>
 * 版本更新：
 * 更新版本号    更新人      更新时间      更新内容
 */
@Service
public class PromoCodeServiceImpl extends ServiceImpl<PromoCodeMapper, PromoCode> implements PromoCodeService {

    @Resource
    private PromotionGoodsService promotionGoodsService;
    @Resource
    private CouponService couponService;

    /**
     * 保存，需要先校验优惠码是否存在
     *
     * @param couponVO 参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveByCoupon(CouponVO couponVO) {
        // 保存分销人员等级
        // 优惠码保存
        if (CharSequenceUtil.isNotBlank(couponVO.getPromoCode())
                && CharSequenceUtil.isNotBlank(couponVO.getDistributionId())) {
            // 先校验优惠码是否已存在数据库
            if (ObjectUtil.isNotEmpty(getByPromoCode(couponVO.getPromoCode()))) {
                throw new ServiceException(ResultCode.PROMO_CODE_EXIST);
            }
            PromoCode promoCode = new PromoCode();
            promoCode.setPromoCode(couponVO.getPromoCode());
            promoCode.setDistributionId(couponVO.getDistributionId());
            promoCode.setCouponId(couponVO.getId());
            promoCode.setDistributionGrade(couponVO.getDistributionGrade());
            this.save(promoCode);

            // 更新优惠码标志为True
            couponService.updatePromoCodeFlagTrue(couponVO.getId());
            promotionGoodsService.updatePromoCodeFlagTrue(couponVO.getId());
        }
    }

    @Override
    public PromoCode getByPromoCode(String promoCode) {
        if (CharSequenceUtil.isBlank(promoCode)) {
            return null;
        }
        return this.getOne(Wrappers.<PromoCode>lambdaQuery()
                .eq(PromoCode::getPromoCode, promoCode));
    }

    /**
     * 根据优惠券ID查询
     *
     * @param couponId 优惠券ID
     * @return 优惠码
     */
    @Override
    public PromoCode getByCouponId(String couponId) {
        if (CharSequenceUtil.isBlank(couponId)) {
            return null;
        }
        return this.getOne(Wrappers.<PromoCode>lambdaQuery()
                .eq(PromoCode::getCouponId, couponId));
    }

    /**
     * 根据优惠券ID列表查询
     *
     * @param couponIds 优惠券ID列表
     * @return 优惠码
     */
    @Override
    public List<PromoCode> listByCouponIds(List<String> couponIds) {
        if (CollUtil.isEmpty(couponIds)) {
            return new ArrayList<>();
        }
        return this.list(Wrappers.<PromoCode>lambdaQuery()
                .in(PromoCode::getCouponId, couponIds));
    }
}
